﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;

namespace MyHealthOnline.Common
{
    public partial class ViewQuestionList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public QuestionCollection DataSource
        {
            set
            {
                rptQuestions.DataSource = value;
                rptQuestions.DataBind();
            }
        }

        public bool IsOpenQuestion(string type)
        {
            return (type == "Open");
        }

        public bool IsMultiChoiceQuestion(string type)
        {
            return (type == "Multi");
        }

        public bool IsDateQuestion(string type)
        {
            return (type == "Date");
        }

        protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                if (question.Type == "Open")
                {
                    Label lblOpenQuestion = e.Item.FindControl("lblOpenQuestion") as Label;
                    lblOpenQuestion.Text = question.QuestionText;

                    Label lblOpenQuestionAnswer = e.Item.FindControl("lblOpenQuestionAnswer") as Label;
                    lblOpenQuestionAnswer.Text = question.OpenAnswer;
                }
                else if (question.Type == "Multi")
                {
                    Label lblMultiQuestion = e.Item.FindControl("lblMultiQuestion") as Label;
                    lblMultiQuestion.Text = question.QuestionText;

                    Label lblMultiQuestionAnswer = e.Item.FindControl("lblMultiQuestionAnswer") as Label;
                    lblMultiQuestionAnswer.Text = question.MultiAnswer;
                }
                else if (question.Type == "Date")
                {
                    Label lblDateQuestion = e.Item.FindControl("lblDateQuestion") as Label;
                    lblDateQuestion.Text = question.QuestionText;

                    Label lblDateQuestionAnswer = e.Item.FindControl("lblDateQuestionAnswer") as Label;
                    lblDateQuestionAnswer.Text = (question.DateAnswer.HasValue) ?
                                                    question.DateAnswer.Value.ToShortDateString() :
                                                    "";
                }
            }
        }
    }
}